package com.maikec.utils.file;
/**
 * 邢帅教育
 * <p>
 * 本源代码由数据中心及其作者共同所有，未经版权持有者的事先书面授权，
 * 不得使用、复制、修改、合并、发布、分发和/或销售本源代码的副本。
 *
 * @copyright Copyright (c) 2018. [图片]xsteach.com all rights reserved.
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传下载
 * @author maikec
 * @date 2018/7/28
 */
@RestController
@RequestMapping("file")
@Slf4j
public class FileController{
    private final FileService fileService;
    public FileController(FileService fileService) {
        this.fileService = fileService;
    }
    @GetMapping("getFilePage")
    public ModelAndView getFilePage() {
        ModelAndView modelAndView = new ModelAndView("uploadForm");
        return modelAndView;
    }
    @PostMapping(value = "upload")
    public Map upload(@RequestParam("file") MultipartFile multipartFile){
        Map result = new HashMap(3);
        result.put("obj",fileService.saveFile(multipartFile));
        return result;
    }
    @GetMapping(value = "download")
    public String download(@RequestParam("id") int fileId){
        File file = fileService.getFile(fileId);
        StringBuffer result = new StringBuffer("data:"+file.getContentType()+";base64,");
        result.append(file.getContent());
        return result.toString();
    }
}
